#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/legacy/legacy.hpp"

#include <iostream>
#include <stdio.h>
#include "debug.h"

int main() {
#if 0
	int ret =cvcamSelectCamera();
	D(DEBUG_ERR, "[%d]", ret);
#endif
    CvCapture* capture = NULL;
	capture = cvCaptureFromCAM(CV_CAP_V4L2 + 0);
	if (!capture) {
		D(DEBUG_ERR, "[%p] open cap error", capture);
		return -1;
	}
	char buff[16] = {0};
	int ret = -1;
	for(int i = 0; i < 10; i++) {
		IplImage* iplImg = cvQueryFrame( capture );
		D(DEBUG_INF,"[%d][%d][%d]", iplImg->width, iplImg->height, iplImg->imageSize);
		sprintf(buff, "/tmp/%d.jpg", i);
		D(DEBUG_INF, "[%s]", buff);
		FILE* pf = fopen(buff, "w+");
		if (pf) {
			ret = fwrite(iplImg->imageData, 1, iplImg->imageSize, pf);
			fclose(pf);
		}
	}
    cvReleaseCapture( &capture );
	return 0;
}
